In [1]:
import warnings
warnings.filterwarnings("ignore")
Configuration¶
In [2]:
import scanpy as sc
import scvi
import anndata as ad
import torch
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import os
# Make sure plots appear inline in notebooks
%matplotlib inline
# Set Scanpy plotting settings
sc.settings.set_figure_params(dpi=100, facecolor='white', frameon=False)
# Set scvi-tools settings
scvi.settings.seed = 0
print("scvi-tools version:", scvi.__version__)
print("PyTorch version:", torch.__version__)
# --- Configuration ---
N_HVG = 3000 # Number of highly variable genes to select
N_NEIGHBORS = 15 # Number of neighbors for KNN graph
N_PCS = 30 # Number of PCs for KNN graph (used before scVI)
LEIDEN_RESOLUTION = 0.6 # Resolution for Leiden clustering
SCVI_MAX_EPOCHS = 400 # Maximum training epochs for scVI (can be lowered for speed)
Seed set to 0
scvi-tools version: 1.3.0 PyTorch version: 2.6.0+cu124
In [3]:
sampleid = "Explanted2"
results_dir = "./102_Scvi_visium_results_"+sampleid+"/"
os.makedirs(results_dir, exist_ok=True)
adata = sc.read_h5ad("./101_Preprocess_processed_adata/QC_processed_"+sampleid+".h5ad")
print("Original AnnData object:")
print(adata)
Original AnnData object:
AnnData object with n_obs × n_vars = 1158 × 18074
obs: 'in_tissue', 'array_row', 'array_col', 'n_genes_by_counts', 'total_counts', 'total_counts_mt', 'pct_counts_mt', 'pass_qc'
var: 'gene_ids', 'feature_types', 'genome', 'mt', 'n_cells_by_counts', 'mean_counts', 'pct_dropout_by_counts', 'total_counts'
uns: 'spatial'
obsm: 'spatial'
Step 1: Preserving raw counts...¶
In [4]:
if 'counts' not in adata.layers:
adata.layers['counts'] = adata.X.copy()
else:
print("Layer 'counts' already exists. Assuming it contains raw counts.")
Step 2: Basic Quality Control and Filtering¶
In [5]:
# 2a. Filter out spots not 'in_tissue' (usually done by default by spaceranger/read_visium)
if 'in_tissue' in adata.obs.columns:
n_spots_before = adata.n_obs
adata = adata[adata.obs['in_tissue'] == 1, :]
print(f" Filtered out {n_spots_before - adata.n_obs} spots not in tissue.")
else:
print(" 'in_tissue' column not found, assuming all spots are in tissue.")
Filtered out 0 spots not in tissue.
In [6]:
adata.layers['counts'] = adata.X.copy() # Ensure layer 'counts' has filtered raw counts
adata.raw = adata.copy() # Store the filtered data with raw counts in .X into .raw
Step 3: QC¶
In [7]:
sc.pl.violin(adata, ["n_genes_by_counts", "total_counts", "pct_counts_mt"], jitter=0.4, multi_panel=True)
Step 4: Prepare Data for scVI¶
In [8]:
# 4a. Normalize total counts per spot and log-transform (for HVG selection and visualization)
adata_for_hvg = adata.copy() # Work on a copy for HVG selection
sc.pp.normalize_total(adata_for_hvg, target_sum=1e4)
sc.pp.log1p(adata_for_hvg)
In [9]:
# 4b. Identify Highly Variable Genes (HVGs) using the log-normalized data
print(f" Selecting top {N_HVG} Highly Variable Genes...")
sc.pp.highly_variable_genes(
adata_for_hvg,
n_top_genes=N_HVG,
subset=False, # Don't subset yet, just mark them
flavor="seurat_v3", # Common flavor, works well
layer=None # Use adata_for_hvg.X which is log-normalized
)
Selecting top 3000 Highly Variable Genes...
In [10]:
# Transfer the HVG information back to the original adata object
adata.var['highly_variable'] = adata_for_hvg.var['highly_variable']
adata.var['highly_variable_rank'] = adata_for_hvg.var['highly_variable_rank']
adata.var['means'] = adata_for_hvg.var['means']
adata.var['variances'] = adata_for_hvg.var['variances']
adata.var['variances_norm'] = adata_for_hvg.var['variances_norm']
print(f" Marked {adata.var['highly_variable'].sum()} genes as highly variable.")
Marked 3000 genes as highly variable.
Step 5: Setup and Train scVI Model¶
In [11]:
# 5a. Setup AnnData object for scvi-tools
# Tell scVI to use the raw counts stored in the 'counts' layer
# It will automatically use the 'highly_variable' column in adata.var if setup correctly
scvi.model.SCVI.setup_anndata(
adata,
layer="counts", # Use the raw counts layer we created
# Optional: Add batch key if needed, e.g., batch_key="sample_id"
# Optional: Add categorical covariates if relevant, e.g., categorical_covariate_keys=["cell_type"]
)
In [12]:
# 5b. Initialize the scVI model
# n_latent: Dimensionality of the latent space (adjust if needed, 10-30 often works well)
# n_layers: Number of hidden layers in the neural network (1 or 2 is common)
vae = scvi.model.SCVI(adata, n_layers=2, n_latent=30, gene_likelihood="zinb") # ZINB recommended for UMI counts
In [13]:
# 5c. Train the scVI model
# use_gpu=USE_GPU will automatically use GPU if available and specified
# plan_kwargs can control learning rate, weight decay, etc.
# check_val_every_n_epoch=10 helps with early stopping monitoring
print(f" Training scVI model for up to {SCVI_MAX_EPOCHS} epochs...")
vae.train(max_epochs=SCVI_MAX_EPOCHS, plan_kwargs={'lr': 1e-3}, check_val_every_n_epoch=10)
GPU available: True (cuda), used: True
TPU available: False, using: 0 TPU cores
HPU available: False, using: 0 HPUs
You are using a CUDA device ('NVIDIA GeForce RTX 4060 Ti') that has Tensor Cores. To properly utilize them, you should set `torch.set_float32_matmul_precision('medium' | 'high')` which will trade-off precision for performance. For more details, read https://pytorch.org/docs/stable/generated/torch.set_float32_matmul_precision.html#torch.set_float32_matmul_precision
LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]
Training scVI model for up to 400 epochs...
`Trainer.fit` stopped: `max_epochs=400` reached.
In [14]:
# Extract ELBO loss from training history
history = vae.history
elbo_train = history["elbo_train"] # Training loss
elbo_validation = history["reconstruction_loss_train"] # Validation loss
# Plot convergence curve
plt.figure(figsize=(6, 4))
plt.plot(elbo_train, label="Training Loss", color="blue")
plt.plot(elbo_validation, label="Validation Loss", color="red", linestyle="dashed")
plt.xlabel("Epochs")
plt.ylabel("Negative ELBO")
plt.title("scVI Training Convergence")
plt.legend()
plt.savefig(os.path.join(results_dir, "scvi_training_elbo.png"))
Step 6: Post-Training Analysis - Latent Space, Clustering, Visualization¶
In [15]:
# 6a. Get the latent representation from the trained model
print(" Extracting scVI latent representation...")
adata.obsm["X_scVI"] = vae.get_latent_representation()
Extracting scVI latent representation...
In [16]:
# 6b. Perform clustering on the scVI latent space
print(" Performing Leiden clustering on scVI latent space...")
sc.pp.neighbors(adata, n_neighbors=N_NEIGHBORS, use_rep="X_scVI")
sc.tl.leiden(adata, resolution=LEIDEN_RESOLUTION, key_added="leiden_scvi", flavor="igraph", n_iterations=2)
Performing Leiden clustering on scVI latent space...
In [17]:
# 6c. Compute UMAP embedding based on the scVI latent space
print(" Computing UMAP embedding...")
sc.tl.umap(adata, min_dist=0.3) # min_dist controls spread
Computing UMAP embedding...
In [18]:
# 6d. Visualize the results
print(" Generating visualizations...")
# UMAP colored by cluster
sc.pl.umap(adata, color="leiden_scvi", title="UMAP colored by scVI Leiden Clusters", show=True)
Generating visualizations...
In [19]:
# Spatial plot colored by cluster
sc.pl.spatial(adata, color="leiden_scvi", title="Spatial - scVI Leiden Clusters", show=True, spot_size=140) # Adjust spot_size as needed
In [20]:
# Visualize QC metric spatially (optional)
sc.pl.spatial(adata, color="total_counts", title="Spatial - Total Counts", show=True, spot_size=140, cmap="jet")
In [21]:
sc.pl.spatial(adata, color="n_genes_by_counts", title="Spatial - Genes per Spot", show=True, spot_size=140)
Step 7: Differential Gene Expression (DGE) using scVI¶
In [22]:
de_df = vae.differential_expression(groupby="leiden_scvi")
In [23]:
de_df
Out[23]:
| proba_de | proba_not_de | bayes_factor | scale1 | scale2 | pseudocounts | delta | lfc_mean | lfc_median | lfc_std | ... | raw_mean1 | raw_mean2 | non_zeros_proportion1 | non_zeros_proportion2 | raw_normalized_mean1 | raw_normalized_mean2 | is_de_fdr_0.05 | comparison | group1 | group2 | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| MYH11 | 0.9170 | 0.0830 | 2.402267 | 5.790696e-03 | 1.724554e-03 | 0.001901 | 0.25 | 2.331644 | 2.106627 | 1.523553 | ... | 28.862068 | 3.812846 | 1.000000 | 0.640115 | 75.699799 | 21.157900 | False | 0 vs Rest | 0 | Rest |
| MYL9 | 0.8776 | 0.1224 | 1.969896 | 9.674547e-03 | 3.583499e-03 | 0.001901 | 0.25 | 1.829020 | 1.805245 | 1.240752 | ... | 51.991386 | 8.303257 | 1.000000 | 0.817658 | 136.628067 | 46.909134 | False | 0 vs Rest | 0 | Rest |
| MCAM | 0.8738 | 0.1262 | 1.934983 | 2.247476e-03 | 7.337324e-04 | 0.001901 | 0.25 | 1.952341 | 2.059022 | 1.105960 | ... | 9.456899 | 1.537424 | 1.000000 | 0.523033 | 24.920521 | 8.831893 | False | 0 vs Rest | 0 | Rest |
| C11orf96 | 0.8716 | 0.1284 | 1.915180 | 2.913290e-03 | 9.193522e-04 | 0.001901 | 0.25 | 2.020271 | 2.073465 | 1.278625 | ... | 12.706895 | 1.949129 | 1.000000 | 0.563340 | 32.451939 | 10.980057 | False | 0 vs Rest | 0 | Rest |
| DSTN | 0.8528 | 0.1472 | 1.756733 | 2.906874e-03 | 1.041963e-03 | 0.001901 | 0.25 | 1.855628 | 1.846919 | 1.238593 | ... | 11.560346 | 2.163139 | 0.991379 | 0.577735 | 30.916525 | 12.180758 | False | 0 vs Rest | 0 | Rest |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| DEPDC1B | 0.0000 | 1.0000 | -0.000000 | 7.787964e-08 | 1.187874e-07 | 0.001833 | 0.25 | -0.029966 | -0.024473 | 0.108588 | ... | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | False | 4 vs Rest | 4 | Rest |
| ZBTB8B | 0.0000 | 1.0000 | -0.000000 | 5.922493e-08 | 1.119491e-07 | 0.001833 | 0.25 | -0.038850 | -0.030379 | 0.103872 | ... | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | False | 4 vs Rest | 4 | Rest |
| STEAP1B | 0.0000 | 1.0000 | -0.000000 | 3.154829e-08 | 5.099137e-08 | 0.001833 | 0.25 | -0.014871 | -0.012353 | 0.065281 | ... | 0.000000 | 0.002116 | 0.000000 | 0.001058 | 0.000000 | 0.005194 | False | 4 vs Rest | 4 | Rest |
| MKRN2OS | 0.0000 | 1.0000 | -0.000000 | 7.027223e-08 | 1.213345e-07 | 0.001833 | 0.25 | -0.036840 | -0.025311 | 0.103569 | ... | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | False | 4 vs Rest | 4 | Rest |
| OR8S1 | 0.0000 | 1.0000 | -0.000000 | 4.382172e-08 | 1.022494e-07 | 0.001833 | 0.25 | -0.042789 | -0.024605 | 0.094510 | ... | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | False | 4 vs Rest | 4 | Rest |
90370 rows × 22 columns
In [24]:
de_df = de_df[de_df.proba_de > 0.85]
de_df = de_df[de_df.lfc_mean > 1.0]
In [25]:
de_df = de_df.sort_values(by=["group1", "proba_de", "lfc_mean"], ascending=[True, False, False])
dge_filename = os.path.join(results_dir, "scvi_differential_expression.csv")
de_df.to_csv(dge_filename)
print(f" Differential expression results saved to '{dge_filename}'")
Differential expression results saved to './102_Scvi_visium_results_Explanted2/scvi_differential_expression.csv'
In [26]:
print("\n Top markers per cluster (based on scVI DGE):")
n_top_markers = 5
for cluster_id in sorted(de_df['group1'].unique()):
print(f"\n --- Cluster {cluster_id} vs Rest ---")
top_markers = de_df[de_df['group1'] == cluster_id].head(n_top_markers)
if top_markers.empty:
print(" No significant markers found with current thresholds.")
else:
print(top_markers[['proba_de', 'lfc_mean', 'non_zeros_proportion1']]) # Show key stats
Top markers per cluster (based on scVI DGE):
--- Cluster 0 vs Rest ---
proba_de lfc_mean non_zeros_proportion1
MYH11 0.9170 2.331644 1.000000
MYL9 0.8776 1.829020 1.000000
MCAM 0.8738 1.952341 1.000000
C11orf96 0.8716 2.020271 1.000000
DSTN 0.8528 1.855628 0.991379
--- Cluster 1 vs Rest ---
proba_de lfc_mean non_zeros_proportion1
FABP4 0.9178 3.484967 0.731959
CFD 0.8870 2.950568 0.881443
--- Cluster 2 vs Rest ---
proba_de lfc_mean non_zeros_proportion1
FN1 0.8578 1.773607 0.961905
--- Cluster 3 vs Rest ---
proba_de lfc_mean non_zeros_proportion1
VCAN 0.8948 2.666103 0.958333
AEBP1 0.8916 2.415082 0.973214
BGN 0.8858 2.237476 0.976190
IGFBP7 0.8838 1.451984 0.997024
FN1 0.8668 1.684646 1.000000
Step 8: Visualizing top marker genes spatially (optional)...¶
In [27]:
# Get some top markers across a few clusters
markers_to_plot = []
for cluster_id in sorted(de_df['group1'].unique())[:3]: # Plot for first 3 clusters
cluster_markers = de_df[de_df['group1'] == cluster_id].head(2).index.tolist()
if cluster_markers:
markers_to_plot.extend(cluster_markers)
markers_to_plot = list(dict.fromkeys(markers_to_plot)) # Unique markers
In [28]:
markers_to_plot
Out[28]:
['MYH11', 'MYL9', 'FABP4', 'CFD', 'FN1']
In [29]:
sc.pl.umap(adata, color = markers_to_plot, cmap='jet')
In [30]:
if markers_to_plot:
print(f" Plotting spatial expression for: {markers_to_plot}")
# Use expression from adata.raw for visualization if desired (raw counts)
# Or use normalized data from adata.X
plt.figure()
sc.pl.spatial(
adata,
color=markers_to_plot,
spot_size=180,
cmap = 'jet',
alpha=1,
ncols=min(len(markers_to_plot), 4), # Adjust layout
# layer='counts', # Uncomment to plot raw counts from the layer
use_raw=True,
show=True,
)
plt.close()
print(f" Marker gene spatial plots saved with prefix in '{results_dir}/figures/'")
else:
print(" No top markers found to plot.")
Plotting spatial expression for: ['MYH11', 'MYL9', 'FABP4', 'CFD', 'FN1']
<Figure size 400x400 with 0 Axes>
Marker gene spatial plots saved with prefix in './102_Scvi_visium_results_Explanted2//figures/'
Step 9: Saving final AnnData object and scVI model...¶
In [31]:
adata_filename = os.path.join(results_dir, "processed_visium_adata_scvi.h5ad")
adata.write(adata_filename)
print(f" Processed AnnData object saved to '{adata_filename}'")
model_filename = os.path.join(results_dir, "scvi_model")
vae.save(model_filename, overwrite=True)
print(f" Trained scVI model saved to '{model_filename}'")
Processed AnnData object saved to './102_Scvi_visium_results_Explanted2/processed_visium_adata_scvi.h5ad'
Trained scVI model saved to './102_Scvi_visium_results_Explanted2/scvi_model'